//
//  LoginViewController.swift
//  ys
//
//  Created by Wei Zhang on 2020/10/21.
//  Copyright © 2020 yzh. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class LoginViewController: BaseViewController {
    @IBOutlet weak var usernameTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var loginButton: UIButton!
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.fd_prefersNavigationBarHidden = true
        loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
    }
    
    
    @objc func login(){
        self.view.endEditing(true)
        let username = self.usernameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
        let password = self.passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
        self.view.loading(msg: "登录中")
        NetworkService().login(username: username, password: password).catchError {[unowned self] (error) -> Observable<ZWData> in
            self.view.loadComplete()
            Toast.showInfo(content: "网络连接失败")
            return Observable.empty()
        }.subscribe(onNext: {[unowned self] (data) in
            self.view.loadComplete()
            if data.code == 200 || data.code == 201{
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: kLOGIN_SUCCESS), object: nil)
            }else{
                Toast.showInfo(content: data.msg ?? "")
            }
            }).disposed(by: disposeBag)
    }
    
    @IBAction func privateBtnClicked(_ sender : UIButton){
        let cont = WebViewController.init()
        cont.title = "隐私政策"
        cont.url = "https://www.baidu.com"
        self.navigationController?.pushViewController(cont, animated: true)
    }
    
    @IBAction func agreementBtnClicked(_ sender : UIButton){
        let cont = WebViewController.init()
        cont.title = "用户协议"
        cont.url = "https://www.baidu.com"
        self.navigationController?.pushViewController(cont, animated: true)
    }
}
